package offer;

/**
 * @Author SSK
 * @Date Created in 22:17 2019/8/6
 * @Description 删除链表中重复的结点
 * @Version 1.0
 */
public class T57 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode deleteDuplication(ListNode pHead) {
        while (pHead!=null&&pHead.next!=null) {

            if (pHead.val==pHead.next.val) {
                int temp = pHead.val;
                while (pHead!=null&&pHead.val==temp) {
                    pHead = pHead.next;
                }

            }else {
                break;
            }
        }
        if (pHead==null) {
            return pHead;
        }

        ListNode now = pHead;
        ListNode next = pHead.next;
        while (next!=null&&next.next!=null) {
            if (next.val==next.next.val) {
                int temp = next.val;
                while (next!=null&&next.val==temp) {
                    next = next.next;
                }

            }else {

                now.next = next;
                now = next;
                next = now.next;
            }
        }
        return pHead;
    }
    void test (int[] array) {
        ListNode head = new ListNode(array[0]);
        ListNode temp = head;
        for (int i = 1; i < array.length; i++) {
            ListNode listNode = new ListNode(array[i]);
            temp.next = listNode;
            temp = listNode;
        }
        head = deleteDuplication(head);
        System.out.println("--------------------");
        while (head!=null) {
            System.out.println(head.val);
            head = head.next;
        }
    }
    public static void main ( String[] args) {
        new T57().test(new int[]{1,2,3,3,4,4,5});

    }
}
